package raft.entity;


import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;

@Getter
@Setter
public class AentryParam extends  BaseParam {

    /**
     *
     */
    String leaderId;

    /**
     * 前一个索引
     */
    long prevLogIndex;

    /**
     * 前一个任期
     */
    long preLogTerm;

    /**
     * 准备存储的日志条目
     */
    LogEntry[] entries;

    /**
     * 领导提交的索引值
     */
    long leaderCommitIndex;

    public AentryParam(Builder builder){
        setTerm(builder.term);
        setServiceId(builder.serverId);
        setLeaderId(builder.leaderId);
        setPrevLogIndex(builder.prevLogIndex);
        setPreLogTerm(builder.preLogTerm);
        setEntries(builder.entries);
        setLeaderCommitIndex(builder.leaderCommitIndex);
    }

    public AentryParam() {

    }


    public static Builder newBuilder(){
        return new Builder();
    }

    public static final class Builder{
        private long term;
        private String serverId;
        private String leaderId;
        private long prevLogIndex;
        private long preLogTerm;
        private LogEntry[] entries;
        private long leaderCommitIndex;

        private Builder() {
        }

        public Builder term(long val) {
            term = val;
            return this;
        }

        public Builder serverId(String val) {
            serverId = val;
            return this;
        }

        public Builder leaderId(String val) {
            leaderId = val;
            return this;
        }

        public Builder prevLogIndex(long val) {
            prevLogIndex = val;
            return this;
        }

        public Builder preLogTerm(long val) {
            preLogTerm = val;
            return this;
        }

        public Builder entries(LogEntry[] val) {
            entries = val;
            return this;
        }

        public Builder leaderCommit(long val) {
            leaderCommitIndex = val;
            return this;
        }

        public AentryParam build() {
            return new AentryParam(this);
        }
    }



    @Override
    public String toString() {
        return "AentryParam{" +
                "leaderId='" + leaderId + '\'' +
                ", prevLogIndex=" + prevLogIndex +
                ", preLogTerm=" + preLogTerm +
                ", entries=" + Arrays.toString(entries) +
                ", leaderCommitIndex=" + leaderCommitIndex +
                '}';
    }
}
